package cn.edu.neusoft.food229.adapter;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.food229.Commen.Commen;
import cn.edu.neusoft.food229.R;
import cn.edu.neusoft.food229.activity.FoodActivity;
import cn.edu.neusoft.food229.activity.PurchaseActivity;
import cn.edu.neusoft.food229.bean.FoodBean;
import cn.edu.neusoft.food229.model.CarModel;

import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;

public class FoodAdapter extends RecyclerView.Adapter {
    private List<FoodBean> list;
    private Context context;
    private LayoutInflater layoutInflater;
    public FoodAdapter(Context context, List<FoodBean>list){
        this.context=context;
        this.list=list;
        layoutInflater=LayoutInflater.from(context);
    }
    public void  setList(List<FoodBean> list){
        this.list=list;
        notifyDataSetChanged();
    }
    public class ViewHolder extends  RecyclerView.ViewHolder{

        ImageView imageView;
        TextView tv_name,tv_price,tv_intro;
        Button button;
//        ImageButton imageButton;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            imageView=itemView.findViewById(R.id.imageView9);
            tv_name=itemView.findViewById(R.id.textView9);
            tv_price=itemView.findViewById(R.id.textView10);
            tv_intro=itemView.findViewById(R.id.textView14);
            button=itemView.findViewById(R.id.button3);
//            imageButton=itemView.findViewById(R.id.btn_shopcar);
        }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View v=layoutInflater.from(layoutInflater.getContext()).inflate(R.layout.food_item,parent,false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
       final FoodBean bean=list.get(position);
       if(bean==null)return;
        ViewHolder viewHolder=(ViewHolder) holder;
        Picasso.with(context)
                .load(Commen.BASEURL+bean.getPic())
                .placeholder(R.mipmap.ic_launcher)
                .into(viewHolder.imageView);

        viewHolder.tv_name.setText(bean.getFoodname());

        //doule转String
        String s=""; double price=bean.getPrice();
        s=String.valueOf(price);
       viewHolder.tv_price.setText(s);//---放一个String类型数据


        viewHolder.tv_intro.setText(bean.getIntro());
        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "-----"+bean.getIntro()+"  简介："+bean.getIntro(), Toast.LENGTH_SHORT).show();
               Intent intent = new Intent(context, FoodActivity.class);
                intent.putExtra("food_id",bean.getFood_id());
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);

            }
        });
        final String finalS = s;
        viewHolder.button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                notifyDataSetChanged();
                Toast.makeText(context, "购买", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(context, PurchaseActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//-----------------------------传数据------------------------------
                intent.putExtra("food_id",bean.getFood_id());
                intent.putExtra("food_name",bean.getFoodname());
               intent.putExtra("price", finalS);
                intent.putExtra("pic",bean.getPic());
 //----------------------------------------------------------------
                context.startActivity(intent);
            }
        });
//        viewHolder.imageButton.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
////                String u_id,f_id; int nunmber;
////                f_id=bean.getFood_id();
////                CarModel model=new CarModel();
//////                model.addCar(u);
//                Intent intent = new Intent(context, PurchaseActivity.class);
//                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                context.startActivity(intent);
//            }
//        });

    }

    @Override
    public int getItemCount() {
        if(list!=null){
            return list.size();}//若使用对象是，避免出现空指针
        else
            return 0;//空条目是返回0
    }
}
